<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改轮播管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-carousel-edit" th:object="${carousel}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group ">
                <div class="form-group">
                    <label class="col-sm-3 control-label">类型选择：</label>
                    <div class="col-sm-8">
                        <select name="reserve" id="reserve" class="form-control m-b"  th:field="*{reserve}" disabled>
                            <option value="0">跳转店铺</option>
                            <option value="1">跳转外网</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">上传图片：</label>
                <input id="img" th:field="*{imgUrl}" hidden>
                <div class="col-sm-8">
                    <input id="imgFile" name="imgFile" class="file" type="file"  data-theme="fas" accept=".jpg,.img,.png">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">序号：</label>
                <div class="col-sm-8">
                    <input name="status" th:field="*{status}" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group" th:if="*{reserve} eq ('0')">
                <label class="col-sm-3 control-label">店铺：</label>
                <input name="shopId" id="shopId" th:field="*{shopId}" type="text" hidden>
                <div class="col-sm-8">
                    <input  id="shopName" th:field="*{shopName}" class="form-control" type="text" onclick="selectShop()">
                </div>
            </div>
            <div class="form-group" id="waiWang" th:if="*{reserve} eq ('1')">
                <label class="col-sm-3 control-label">外网地址：</label>
                <div class="col-sm-8">
                    <input name="seatId" th:field="*{seatId}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="comments" th:field="*{comments}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "carousel/carousel";
        $("#form-carousel-edit").validate({
            onkeyup: false,
            rules:{
                status:{
                    remote: {
                        url: prefix + "/checkStatusUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "status": function () {
                                return $.common.trim($("#status").val());
                            },
                            "id": function () {
                                return $.common.trim($("#id").val());
                            }

                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "status":{
                    remote: "序号重复"
                }
            },
            focusCleanup: true
        });

        var value = document.getElementById('img').value;
        var imgArray = [];
        imgArray.push(ctx+value);
        $(function () {
            //图片回显
            $("#imgFile").fileinput('destroy');
            $("#imgFile").fileinput({
                language: 'zh', //设置语言
                allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
                uploadAsync: false, //默认异步上传
                showUpload:false,//是否显示上传按钮
                showUpload: false, //是否显示上传按钮
                showRemove: false, //显示移除按钮
                showPreview: true, //是否显示预览
                showCaption: true,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                initialPreviewAsData: true,
                initialPreviewFileType: 'image',
                initialPreview: imgArray,
                enctype: 'multipart/form-data',
                validateInitialCount: true
            });
            //删除无用按钮
            $(".file-actions").remove()
        })



        function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData($("#form-carousel-edit")[0]);//收集表单数据
                if ($('#imgFile')[0].files[0] != null) {
                    formData.append('imgFile', $('#imgFile')[0].files[0]);
                }
                $.ajax({
                    url: prefix + "/edit",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (result) {
                        $.operate.successCallback(result);
                    }
                });
            }
        }

        function selectShop() {
            var url=prefix+"/selectShop";
            $.modal.open("选择店铺",url);
        }
    </script>
</body>
</html>